﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Data;

using CqusInfo.RAL.User;
using CqusInfo.CR.SqlHelper;

namespace CqusInfo.DAL.User
{
    public class User_InfoToLoveDAL:Interface.IUser_InfoToLoveDAL
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public User_InfoToLoveDAL()
        {

        }
 

        public List<User_InfoToLoveModel> Query_ByUserName(string userName)
        {
            List<User_InfoToLoveModel> userList = new List<User_InfoToLoveModel>();
            DataSet ds = SQLiteHelper.Query("tb_User_InfoToLove", "c_vc_uitlId", userName, "c_b_isDel", false);
            string cacheId = string.Format("User_InfoToLove_{0}", userName);
            userList = SqlData.GetQueryList<User_InfoToLoveModel>(cacheId, "User_InfoToLoveModel", ds);

            return userList;
        }

        /// <summary>
        /// 得到数据参数
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="model"></param>
        /// <returns>parameters</returns>
        private SQLiteParameter[] GetParameter(string userName, User_InfoToLoveModel model)
        {
            SQLiteParameter[] parameters = { 
                                       new SQLiteParameter("@userName",DbType.String),
                                       new SQLiteParameter("@love",DbType.String),
                                       new SQLiteParameter("@star",DbType.String),
                                       new SQLiteParameter("@music",DbType.String),
                                       new SQLiteParameter("@movie",DbType.String),
                                       new SQLiteParameter("@sports",DbType.String),
                                       new SQLiteParameter("@game",DbType.String),
                                       new SQLiteParameter("@book",DbType.String),
                                       new SQLiteParameter("@digital",DbType.String),
                                       new SQLiteParameter("@food",DbType.String),
                                       new SQLiteParameter("@travel",DbType.String),
                                       new SQLiteParameter("@other",DbType.String)
                                       };
            parameters[0].Value = userName;
            parameters[1].Value = model.c_vc_love;
            parameters[2].Value = model.c_vc_star;
            parameters[3].Value = model.c_vc_music;
            parameters[4].Value = model.c_vc_movie;
            parameters[5].Value = model.c_vc_sports;
            parameters[6].Value = model.c_vc_game;
            parameters[7].Value = model.c_vc_book;
            parameters[8].Value = model.c_vc_digital;
            parameters[9].Value = model.c_vc_food;
            parameters[10].Value = model.c_vc_travel;
            parameters[11].Value = model.c_tx_other;

            return parameters;
        }

        public Int32 Add(string userName, User_InfoToLoveModel model)
        {
            StringBuilder sqlStr = new StringBuilder();

            sqlStr.Append("insert into tb_User_InfoToLove values(");
            sqlStr.Append("c_vc_uitlId=@userName,c_vc_love=@love,c_vc_star=@star,c_vc_music=@music,c_vc_sports=@sports,c_vc_book=@book,c_vc_digital=@ditigal,c_vc_food=@food,c_vc_travel=@travel,c_vc_other=@other)");
            SQLiteParameter[] parameters = GetParameter(userName, model);

            return SQLiteHelper.ExecuteNonQuery(sqlStr.ToString(), parameters);
        }

        public Int32 Update(string userName, User_InfoToLoveModel model)
        {
            StringBuilder sqlStr = new StringBuilder();

            sqlStr.Append("update tb_User_InfoToLove set ");
            sqlStr.Append("c_vc_love=@love,c_vc_star=@star,c_vc_music=@music,c_vc_sports=@sports,c_vc_book=@book,c_vc_digital=@ditigal,c_vc_food=@food,c_vc_travel=@travel,c_vc_other=@other where c_vc_uitlId=@userName)");
            SQLiteParameter[] parameters = GetParameter(userName, model);

            return SQLiteHelper.ExecuteNonQuery(sqlStr.ToString(), parameters);
        }

        public Int32 Delete(string userName)
        {
            return SQLiteHelper.Delete("tb_User_InfoToLove", "c_vc_uitlId", userName);
        }
    }
}
